﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var _type = "", _typeid = "";
    var PStructId = "";
    $(function () {
        InitialPage();
        GetTree();
        GetGrid();
        $("#cb_gridTable").change(function () {
            GetSum();
        })
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 141.5);
            }, 200);
            e.stopPropagation();
        });

        $("#time_horizon").ComboBox({
            data: [{ "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            text: "value",
            //description: "==请选择==",
            click: function (item) {
                var value = item.attr('data-value');
                switch (value) {
                    case "3"://本月
                        $("#SelectedStartTime").text("@DateTime.Now.ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.AddMonths(1).ToString("yyyy年MM月")");
                        $("#Month1").val("@DateTime.Now.Month");
                        $("#Month2").val("@DateTime.Now.AddMonths(1).Month");
                        break;
                    case "4"://上月
                        $("#SelectedStartTime").text("@DateTime.Now.AddMonths(-1).ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.ToString("yyyy年MM月")");
                        $("#Month1").val("@DateTime.Now.AddMonths(-1).Month");
                        $("#Month2").val("@DateTime.Now.Month");
                        break;
                    case "5"://本年
                        $("#SelectedStartTime").text("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy年MM月")");
                        $("#SelectedEndTime").text("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy年MM月")");
                        $("#Month1").val("01");
                        $("#Month2").val("12");
                        break;
                    default:
                        break;
                }
                $('#btn_Search').trigger("click");
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);

        $("#DateType").ComboBox({
            data: [{ "key": 1, "value": "应收年月" }, { "key": 2, "value": "权责年月" }],
            id: "key",
            text: "value",
        });
        $("#DateType").ComboBoxSetValue(1);
        $("#IsAudit").ComboBox({
            data: [{ "key": 0, "value": "未审核" }, { "key": 1, "value": "已审核" }],
            id: "key",
            text: "value",
            description: "==所有==",
        });
        $("#DateType").ComboBoxSetValue(-1);
        $("#ItemId").ComboBox({
            url: "../../FinancialManage/FeeItems/GetTreeFeeItemsList",
            id: "id",
            text: "text",
            description: "==收费项目==",

        });
        $(".input-wdatepicker").css("background", "#FFF");
    }
    //加载树
    function GetTree() {
        var item = {
            height: $(window).height() - 24.5,
            url: "../../FinancialManage/ReceivableFeeItems/GetTree?IsSubstep=true",
            onnodeclick: function (item) {
                if (item.id == "All") {
                    PStructId = "";
                }
                if (item.type == "PStruct") {
                    PStructId = item.id;
                    if (item.PsType >= 5) {
                        GetFeeInfo();
                    }

                }
                if (item.type == "Vendor") {
                    PStructId = "";
                }
                $('#btn_Search').trigger("click");
            }
        };
        $("#itemTree").treeview(item);
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {
            DateType: $("#DateType").attr("data-value"),
            StartTime: $("#Year1").val() + $("#Month1").val(),
            EndTime: $("#Year2").val() + $("#Month2").val(),
            PStructId: PStructId,
            ItemId: $("#ItemId").attr("data-value"),
        };
        $gridTable.jqGrid({
            url: "../../FinancialManage/ReceivableFeeItems/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 191.5,
            autowidth: true,
            multiselect: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '状态', name: 'State', hidden: true },
                { label: "费项名称", name: "ItemId", width: 200, align: "center", sortable: false },
                {
                    label: "应收年月", name: "ReceiveMonth", width: 120, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return cellvalue.substring(0, 4) + "年" + cellvalue.substring(4, 6) + "月";
                    }
                },
                { label: "计费起始日", name: "BeginDate", width: 120, align: "center", sortable: false, formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                { label: "计费终止日", name: "EndDate", width: 120, align: "center", sortable: false, formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                { label: "应收金额", name: "ReceiveAmount", width: 150, align: "center", sortable: false },
                 {
                     label: "已收金额", name: "ReceivedAmount", width: 100, align: "center", sortable: false,
                     formatter: function (cellvalue, options, rowObject) {
                         return !!cellvalue ? cellvalue : 0;
                     }
                 },
                {
                    label: "未收金额", name: "UncollectedAmount", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return rowObject.ReceiveAmount - rowObject.ReceivedAmount;
                    }
                },
                { label: "附加说明", name: "Description", width: 200, align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100, 500, 1000],
            pager: "#gridPager",
            sortname: 'CreateDate desc',
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
                var State = $gridTable.getCell(status, "State");
                if (State >= 1) {
                    top.dialogTop('该费项，已加入其他收款', 'error');
                    $gridTable.jqGrid("setSelection", status, false);
                    return false;
                }
                GetSum();

            },
            gridComplete: function () {
                TotalUncollectedAmount();
            },

        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                DateType: $("#DateType").attr("data-value"),
                StartTime: $("#Year1").val() + $("#Month1").val(),
                EndTime: $("#Year2").val() + $("#Month2").val(),
                PStructId: PStructId,
                ItemId: $("#ItemId").attr("data-value"),
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../FinancialManage/ReceivableFeeItems/GetPageList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
    }
    //本次已选
    function GetSum() {
        var $gridTable = $('#gridTable');
        var obj = $gridTable.jqGrid("getRowData");
        var TotalSum = 0.00; //本次已选
        for (var i = 1; i <= obj.length; i++) {
            var c = $("#" + i).attr("class").indexOf("ui-state-highlight");
            if (c >= 0) {
                var ReceiveAmount = parseFloat($gridTable.getCell(i, "ReceiveAmount"));
                TotalSum += ReceiveAmount;
            }
        }
        $("#TotalSum").text(parseFloat(TotalSum));
    }
    //所有未收
    function TotalUncollectedAmount() {
        var $gridTable = $('#gridTable');
        var obj = $gridTable.jqGrid("getRowData");
        var TotalUncollectedAmount = 0.00;
        for (var i = 1; i <= obj.length; i++) {
            TotalUncollectedAmount += parseFloat($gridTable.getCell(i, "UncollectedAmount"));
        }
        $("#TotalUncollectedAmount").text(parseFloat(TotalUncollectedAmount));
    }
    function GetFeeInfo() {
        if (!!PStructId) {
            $.SetForm({
                url: "../../FinancialManage/ReceiptFee/GetFeeInfo",
                param: { keyValue: PStructId },
                success: function (data) {
                    $("#CustomerName").val(data.CName);
                    $("#Adderr").val(data.AllName);
                }

            });
        }
    }

    function btn_receipt() {
        var postData = [];
        var $gridTable = $('#gridTable');
        var CustomerName = $("#CustomerName").val();
        var obj = $gridTable.jqGrid("getRowData");
        for (var i = 1; i <= obj.length; i++) {
            var c = $("#" + i).attr("class").indexOf("ui-state-highlight");
            if (c >= 0) {
                postData.push(obj[i - 1]);
            }
        }
        if (postData.length <= 0) {
            top.dialogTop('至少选择一条收费项', 'error');
            return false;
        }
        if (!!CustomerName) {
            $.ConfirmAjax({
                msg: "注：您确认要进行此操作吗？",
                url: "../../FinancialManage/ReceiptFee/SaveReceiptFee",
                param: { postData: JSON.stringify(postData) },
                success: function (data) {
                    $("#save").attr("disabled", "disabled");

                }
            });
        }
        else {
            top.dialogTop('未选择客户，请选择！', 'error');
            return false;
        }

    }

    function btn_ViewBill() {
        top.tablist.newTab({
            id: "ReceiptBill",
            title: '收款单查看',
            closed: true,
            icon: "fa fa-eye",
            url: top.contentPath + '/FinancialManage/ReceiptBill/Index'
        });
    }
</script>
<div style="position:absolute;top:80px; right:350px;z-index:1000;background:rgba(0, 0, 0, 0); ">
    <div style=" font-size:18px; line-height:20px; text-align:left; float:left;">
        本次已选:&nbsp;
    </div><span id="TotalSum" style=" font-size:18px; line-height:20px; width:120px; font-weight:bold; float:left; text-align:left;">0.00</span>
</div>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            <div id="itemTree"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div style="height:50px; width:99.4%; background-color:#fff;margin-top:10px; border:1px #ccc solid; border-bottom:none;line-height:50px;">
            <table class="form">
                <tr>
                    <td class="formTitle">客户名称</td>
                    <td class="formValue"><input id="CustomerName" readonly type="text" style="border:none;" class="form-control" /></td>
                    <td class="formTitle">房屋地址</td>
                    <td class="formValue" colspan="2"><input id="Adderr" readonly type="text" style="border:none;" class="form-control" /></td>
                    <td class="formTitle">所有未收</td>
                    <td class="formValue"><span id="TotalUncollectedAmount" style=" font-size:18px; line-height:20px; width:120px; font-weight:bold; float:left; text-align:left;">0.00</span></td>
                    <td class="formTitle">预收余额</td>
                    <td class="formValue"><a href="#" style="color:blue;text-decoration:underline;font-weight:bold;">0.00</a></td>
                </tr>
            </table>
        </div>
        <div class="center-Panel" style="margin-top:0px;">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td>筛选条件</td>
                            <td style="padding-left: 10px;">
                                <div class="ui-filter" style="width: 200px;">
                                    <div class="ui-filter-text">
                                        <strong id="SelectedStartTime">@DateTime.Now.ToString("yyyy年MM月")</strong> 至 <strong id="SelectedEndTime">@DateTime.Now.AddMonths(1).ToString("yyyy年MM月")</strong>
                                    </div>
                                    <div class="ui-filter-list" style="width: 400px;">
                                        <table class="form" id="filter-form">
                                            <tr>
                                                <th class="formTitle" style="width: 80px;"><div id="DateType" type="selectTree" class="ui-select" style="width: 80px;"></div></th>
                                                <td class="formValue">
                                                    <input id="Category" type="hidden" value="1" />
                                                    <div style="width: 45%; display: inherit;">
                                                        <input id="Year1" type="text" class="form-control input-wdatepicker" style="width:42%;display:inherit;" onfocus="WdatePicker({ dateFmt: 'yyyy' })" value="@DateTime.Now.ToString("yyyy")" />年
                                                        <input id="Month1" type="text" class="form-control input-wdatepicker" style="width: 32%; display: inherit;" onfocus="WdatePicker({ dateFmt: 'MM' })" value="@DateTime.Now.ToString("MM")" />月
                                                    </div>
                                                    <div style="width: 10%; text-align: center; display: inherit; ">至</div>
                                                    <div style="width: 45%;display:inherit;">
                                                        <input id="Year2" type="text" class="form-control input-wdatepicker" style="width:42%;display:inherit;" onfocus="WdatePicker({ dateFmt: 'yyyy' })" value="@DateTime.Now.AddMonths(1).ToString("yyyy")" />年
                                                        <input id="Month2" type="text" class="form-control input-wdatepicker" style="width: 32%; display: inherit;" onfocus="WdatePicker({ dateFmt: 'MM' })" value="@DateTime.Now.AddMonths(1).ToString("MM")" />月
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle" style="text-align:right;">收费项目：</td>
                                                <td class="formValue">
                                                    <div id="ItemId" type="selectTree" class="ui-select"></div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle" style="text-align:right;">审核状态：</td>
                                                <td class="formValue">
                                                    <div id="IsAudit" type="selectTree" class="ui-select"></div>
                                                </td>
                                            </tr>
                                        </table>
                                        <div class="ui-filter-list-bottom">
                                            <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                            <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-receipt" class="btn btn-default" onclick="btn_receipt()">&nbsp;去收款</a>
                        <a id="lr-receiptadvance" class="btn btn-default" onclick="btn_receiptadvance()">&nbsp;收预收款</a>
                        <a id="lr-viewBill" class="btn btn-default" onclick="btn_ViewBill()">&nbsp;收款单查看</a>
                        <div class="btn-group">
                            <a id="lr-more" class="btn btn-default" data-toggle="dropdown"><i class="fa fa-reorder"></i>&nbsp;更多操作<span class="caret"></span></a>                           
                        </div>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
            </div>
            <div class="gridPanel" style="width:100%;">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>

</div>

